home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / kriegspi / xinput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-30  |  7.3 KB  |  412 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: xinput.c,v 1.8 87/05/19 18:46:30 schoch Exp $";
  3. #endif
  4.  
  5. #include "externs.h"
  6. #include <ctype.h>
  7. #include <strings.h>
  8. #include <sys/signal.h>
  9.  
  10. bool note_on;
  11. bool isiconic;
  12.  
  13. handle_input()
  14. {
  15.     XEvent event;
  16.     XKeyOrButtonEvent *ke;
  17.     static x = -1, y = -1;
  18.     static pos = -1;
  19.     char *cp;
  20.     auto int n;
  21.  
  22.     XNextEvent(&event);
  23.     switch (event.type) {
  24.     case ExposeWindow:
  25.     case ExposeRegion:
  26.         if (event.window == iconwindow) {
  27.         redraw_icon(color == ourcolor);
  28.         isiconic = TRUE;
  29.         } else {
  30.         if (event.type == ExposeRegion) {
  31.             XExposeRegionEvent *xe = (XExposeRegionEvent *)&event;
  32.  
  33.             redraw_region(xe->x, xe->y, xe->width, xe->height);
  34.         } else
  35.             redraw();
  36.         isiconic = FALSE;
  37.         }
  38.         break;
  39.     
  40.     case ButtonPressed:
  41.         ke = (XKeyOrButtonEvent *)&event;
  42.         switch(ke->detail & 0xff) {
  43.         case RightButton:
  44.             break;
  45.         case MiddleButton:
  46.             break;
  47.         case LeftButton:
  48.             x = ke->x; y = ke->y;
  49.             pos = xytopos(x, y);
  50.             startmove(pos, x, y);
  51.             break;
  52.         default:
  53.             printf("Unknown Button pressed.\n");
  54.             break;
  55.         }
  56.         break;
  57.     case ButtonReleased:
  58.         ke = (XKeyOrButtonEvent *)&event;
  59.         switch(ke->detail & 0xff) {
  60.         case LeftButton:
  61.             if (move)
  62.             stopmove(pos, x, y);
  63.             break;
  64.         }
  65.         break;
  66.  
  67.     case LeaveWindow:
  68.         if (pos == -1)
  69.         break;
  70.         pos = -1;
  71.         if (move)
  72.         stopmove(pos, x, y);
  73.         x = -1;
  74.         y = -1;
  75.         break;
  76.  
  77.     case MouseMoved:
  78.         ke = (XKeyOrButtonEvent *)&event;
  79.         pos = xytopos(ke->x, ke->y);
  80.         if (move)
  81.         piecemove(x, y, ke->x, ke->y);
  82.         x = ke->x; y = ke->y;
  83.         break;
  84.  
  85.     case KeyPressed:
  86.         ke = (XKeyOrButtonEvent *)&event;
  87.         cp = XLookupMapping(ke, &n);
  88.         key_func(cp, n);
  89.         break;
  90.  
  91.     default:
  92.         fprintf(stderr, "Unknown event type %d\n", event.type);
  93.         break;
  94.     }
  95. }
  96.  
  97. handle_sock()
  98. {
  99.     char instr[BUFSIZ];
  100.     char *cp;
  101.     int pstart, pend, i;
  102.  
  103.     if (fgets(instr, sizeof instr, inp) == NULL) {
  104.     message("Connection closed.", MESSAGE);
  105.     dead = 1;
  106.     state = OVER;
  107.     return 1;
  108.     }
  109.     if (cp = index(instr, '\n'))
  110.     *cp = '\0';
  111.     if (cp = index(instr, '\r'))
  112.     *cp = '\0';
  113.     i = parse_move(&pstart, &pend, theircolor, instr);
  114.     if (i < 0)    /* Message */
  115.     return 0;
  116.     if (i != TRUE)
  117.     pstart = pend = 0;
  118.     if (movetry(pstart, pend, theircolor) == 0) {
  119.     XFeep(0);
  120.     if (isiconic)
  121.         flash_icon();
  122.     }
  123.     return 0;
  124. }
  125.  
  126. /* Takes window coordinates and returns a board position from 0-99.
  127.  * returns -1 if point is not on board. */
  128. xytopos(x, y)
  129. {
  130.     x += 64; y += 64-TOPSPACE;
  131.     if (y < 0 || x < 0)
  132.     return -1;
  133.     x /= 64;
  134.     y /= 64;
  135.     if (x > 9 || y > 9)
  136.     return -1;
  137.     if (reverse) {
  138.     y = 9 - y;
  139.     x = 9 - x;
  140.     }
  141.     return (y * 10) + x;
  142. }
  143.  
  144. parse_move(pstart, pend, color, instr)
  145. int *pstart, *pend;
  146. u_char color;
  147. char *instr;
  148. {
  149.     char buf[BUFSIZ];
  150.     extern pawntries;    /* in xmovetry.c */
  151.  
  152.     if (!strcmp (instr, "draw")) {
  153.         drawok [color] = TRUE;
  154.         return FALSE;
  155.     }
  156.     if (!strcmp (instr, "resign")) {
  157.         resign = TRUE;
  158.         return FALSE;
  159.     }
  160.     if (drawok [1 - color]) {
  161.         if (!strcmp (instr, "yes")) {
  162.             drawok [color] = TRUE;
  163.         } else if (!strcmp (instr, "no")) {
  164.             ;
  165.         } else {
  166.             printf("error: should get yes or no.\n");
  167.         }
  168.         return FALSE;
  169.     }
  170.     if (!strncmp (instr, "say", 3)) {
  171.         sprintf(buf, "%s:%s", colorname[color], instr+3);
  172.         if (note_on)
  173.             message(buf, CHECK);
  174.         else
  175.             message(buf, MESSAGE);
  176.         return -1;
  177.     }
  178.     if ((!option [ANNOUNCEPAWNS])
  179.     && (!strcmp (instr, "any"))) {
  180.         if (pawntries)
  181.             message ("pawntries", PAWNTRIES);
  182.         else
  183.             message ("no pawntries", PAWNTRIES);
  184.         return -1;
  185.     }
  186.     if (parse_algebraic_move(pstart,pend,instr) == TRUE)
  187.         return TRUE;
  188.  
  189.     printf("Invalid input: %s\n", instr);
  190.     return FALSE;
  191. }
  192.  
  193. #define boardpos(col, row)  ((9 - (row)) * 10 + (col))
  194. #define RIGHT_SIDE 2
  195. #define RIGHT_FILE 1
  196.  
  197. /* parse an algebraic move (e2-e4), and return TRUE for non error */
  198. parse_algebraic_move (pstart, pend, instr)
  199.     int *pstart, *pend;
  200. char *instr;
  201. {
  202.  
  203.     if (instr[2] != '-')
  204.         return FALSE;
  205.     if (instr [0] >= 'a' && instr [0] <= 'h'
  206.     &&  instr [1] >= '1' && instr [1] <= '8'
  207.     &&  instr [3] >= 'a' && instr [3] <= 'h'
  208.     &&  instr [4] >= '1' && instr [4] <= '8') {
  209.         *pstart = boardpos (instr[0]-'a'+1, instr[1]-'0');
  210.         *pend   = boardpos (instr[3]-'a'+1, instr[4]-'0');
  211.         return TRUE;
  212.     } else
  213.         return FALSE;
  214. }
  215.  
  216. key_func(s, n)
  217. char *s;
  218. {
  219.     if (n == 0)
  220.     return;
  221.     if (note_on) {
  222.     if (do_note(s, n))
  223.         note_on = 0;
  224.     return;
  225.     }
  226.     switch(*s) {
  227.     case 'r':
  228.     case 'R':
  229.         replay();
  230.         break;
  231.     case 's':
  232.     case 'S':
  233.         stop_replay();
  234.         break;
  235.     case 'd':
  236.     case 'D':
  237.         draw();
  238.         break;
  239.     case 'q':
  240.     case 'Q':
  241.         do_resign();
  242.         break;
  243.     case 'y':
  244.     case 'Y':
  245.         yes();
  246.         break;
  247.     case 'n':
  248.     case 'N':
  249.         no();
  250.         break;
  251.     case 'm':
  252.     case 'M':
  253.         send_note(s, n);
  254.         break;
  255.     case 'x':
  256.     case 'X':
  257.         xit();
  258.         break;
  259.     case 'a':
  260.     case 'A':
  261.         any();
  262.         break;
  263.     case '?':
  264.     case 'h':
  265.     case 'H':
  266.         help();
  267.         break;
  268.     }
  269. }
  270.  
  271. send_note(s, n)
  272. char *s;
  273. {
  274.     if (dead) {
  275.     message("Your opponent is gone.", MESSAGE);
  276.     return;
  277.     }
  278.     note_on = TRUE;
  279.     s++; n--;
  280.     start_note();
  281.     if (n > 0)
  282.     if (do_note(s, n))
  283.         note_on = 0;
  284. }
  285.  
  286. draw()
  287. {
  288.     if (state != PLAYING) {
  289.     message("The game is over!", MESSAGE);
  290.     return;
  291.     }
  292.     if (color != ourcolor) {
  293.     message("Wait for your turn.", MESSAGE);
  294.     return;
  295.     }
  296.     drawok[ourcolor] = TRUE;
  297.     fputs("draw\r\n", out);
  298.     movetry(0, 0, ourcolor);
  299. }
  300.  
  301. yes()
  302. {
  303.     if (state != PLAYING) {
  304.     message("The game is over!", MESSAGE);
  305.     return;
  306.     }
  307.     if (drawok[theircolor] != TRUE)
  308.     return;
  309.     drawok[ourcolor] = TRUE;
  310.     fputs("yes\r\n", out);
  311.     movetry(0, 0, ourcolor);
  312. }
  313.  
  314. no()
  315. {
  316.     if (state != PLAYING) {
  317.     message("The game is over!", MESSAGE);
  318.     return;
  319.     }
  320.     if (drawok[theircolor] != TRUE)
  321.     return;
  322.     drawok[ourcolor] = FALSE;
  323.     fputs("no\r\n", out);
  324.     movetry(0, 0, ourcolor);
  325. }
  326.  
  327. do_resign()
  328. {
  329.     if (state != PLAYING) {
  330.     message("The game is over!", MESSAGE);
  331.     return;
  332.     }
  333.     fputs("resign\r\n", out);
  334.     resign = TRUE;
  335.     movetry(0, 0, ourcolor);
  336. }
  337.  
  338. xit()
  339. {
  340.     if (state == PLAYING) {
  341.     message("Type Q (resign) or D (draw) first.", MESSAGE);
  342.     return;
  343.     }
  344.     exit(0);
  345. }
  346.  
  347. help()
  348. {
  349.     if (state == REVIEW)
  350.     message("Type S (stop review), X (exit), or M (message)", MESSAGE);
  351.     else if (state == OVER)
  352.     message("Type R (review), or X (exit), or M (message)", MESSAGE);
  353.     else if (drawok[theircolor] && !drawok[ourcolor])
  354.     message("Type Y (accept draw), N (no), or M (message)", MESSAGE);
  355.     else
  356.     message("Type Q (resign), D (draw), or M (message)", MESSAGE);
  357. }
  358.  
  359. replay()
  360. {
  361.     if (state == PLAYING) {
  362.         message("You have to resign first!", MESSAGE);
  363.     } else
  364.         review();
  365. }
  366.  
  367. any()
  368. {
  369.     if (state != PLAYING) {
  370.     message("The game is over!", MESSAGE);
  371.     return;
  372.     }
  373.     if (color != ourcolor) {
  374.     message("It's not your turn.", MESSAGE);
  375.     return;
  376.     }
  377.     if (option[ANNOUNCEPAWNS]) {
  378.     message("The \"announce pawntries\" option is on!", MESSAGE);
  379.     return;
  380.     }
  381.     if (pawntries)
  382.         message ("pawntries", PAWNTRIES);
  383.     else
  384.         message ("no pawntries", PAWNTRIES);
  385.     fputs("any\r\n", out);
  386. }
  387.  
  388. stop_replay()
  389. {
  390.     if (state == REVIEW)
  391.     state = OVER;
  392. }
  393.  
  394. flash_icon()
  395. {
  396.     onalrm(SIGALRM);
  397. }
  398.  
  399. onalrm(sig)
  400. {
  401.     static reverse = FALSE;
  402.  
  403.     if (color != ourcolor || !isiconic) {
  404.     reverse = FALSE;
  405.     return;
  406.     }
  407.     signal(sig, onalrm);
  408.     reverse = !reverse;
  409.     redraw_icon(reverse);
  410.     alarm(1);
  411. }
  412.